package com.rhaggarty.blackjack.game;

import java.math.BigDecimal;

import com.rhaggarty.blackjack.cardroom.Seat;

/**
 * A representation of a single blackjack player.  Note that a player does not maintain
 * a reference to any cards or a position at a blackjack table, but instead only an available amount of 
 * money.  
 * 
 * @see {@link Seat}
 * 
 * @author Ryan Haggarty (ryanmh@gmail.com)
 */
public final class Player {

    private BigDecimal stack;

    public Player(final BigDecimal startingStack) {
        this.stack = startingStack;
    }
    
    public BigDecimal getStack() {
        return stack;
    }
    
    /**
     * Attempts to alter the current player's stack of available chips.
     * 
     * @throws IllegalStateException if the player does not have enough funds for the requested update.
     */
    public void alterStack(final BigDecimal amount) {
        synchronized(stack) {
            final BigDecimal updated = stack.subtract(amount);
 
            if (BigDecimal.ZERO.compareTo(updated) > 0) {
                throw new IllegalStateException("Insufficient Funds!");
            } else {
                this.stack = updated;
            }
        }
    }
}
